package com.gjintel.business.navset.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.business.navset.model.Nav;
import com.gjintel.business.navset.service.INavManageService;
import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.dto.TreeDto;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/business/navmanage")
@Controller
public class NavManageController {

	private static final Logger LOG = Logger.getLogger(NavManageController.class);
	@Autowired
	private INavManageService navManageService;

	/**
	 * @description 获得输入数据信息
	 * @author fengya
	 * @date 2016-3-15 下午03:55:05
	 * @param id
	 * @return
	 * @return ModelAndView`
	 */
	@RequestMapping("/input")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView("/business/navmanage/nav-type-input");
		if (!StringUtil.isEmpty(id)) {
			Nav nav = navManageService.get(Nav.class, id);
			mv.addObject("nav", nav);
		}
		return mv;
	}

	/**
	 * @description 得的所有的记录数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:22
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public JSONArray listAll() {
		List<Nav> list = this.navManageService.findAll();
		return JSONArray.fromObject(list);
	}

	@RequestMapping("/getNavTreeDialog")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject getArticleTreeDialog(String id, String nodeName, String deleteIds) {
		// 树中节点所在的集合
		List<TreeDto> treeList = new ArrayList<TreeDto>();
		try {
			if (nodeName != null) {
				nodeName = URLDecoder.decode(nodeName, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			LOG.debug("汉字转换错误！", e);
		}
		Map jsonData = new HashMap();
		// 查询到的节点ID的集合
		List<String> selIds = this.navManageService.getNavManageTree(treeList, id, nodeName, deleteIds, true);
		jsonData.put("selIds", JSONArray.fromObject(selIds));
		jsonData.put("tree", JSONArray.fromObject(treeList));
		return JSONObject.fromObject(jsonData);
	}

	/**
	 * @description 获得商品类型树
	 * @author fengya
	 * @date 2016-3-15 下午04:04:10
	 * @param id
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/getNavTypeTree")
	@ResponseBody
	public JSONArray getNavTypeTree(String id) {
		// 树中节点所在的集合
		List<TreeDto> treeList = new ArrayList<TreeDto>();
		this.navManageService.getNavManageTree(treeList, id, null, null, false);
		return JSONArray.fromObject(treeList);
	}

	/**
	 * @description 分页查询数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:40
	 * @param page
	 * @param goodsType
	 * @param sort
	 * @return
	 * @return JSONObject
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, Nav nav, Sort sort) {
		this.navManageService.findAll(page, nav, sort);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @description 保存数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:52
	 * @param session
	 * @param goodsType
	 * @return
	 * @return String
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, Nav nav) {
		User user = (User) session.getAttribute("loginUser");
		if (nav != null) {
			boolean sign = false;
			if (StringUtil.isEmpty(nav.getId())) {
				nav.setId(null);
				nav.setCreateTime(new Date());
				nav.setCreaterName(user.getName());
				nav.setCreaterId(user.getId());
				sign = true;
			}
			nav.setUpdateTime(new Date());
			nav.setUpdaterName(user.getName());
			nav.setUpdaterId(user.getId());
			this.navManageService.saveOrUpdate(nav);
			if (sign) {
				LogUtil.addLog(new LogInfo("添加页面导航数据", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()), nav.getClass().getSimpleName(), nav.getId()));
			} else {
				LogUtil.addLog(new LogInfo("更新页面导航数据", user.getId(), user.getName(), String.valueOf(LogType.UPDATE.ordinal()), nav.getClass().getSimpleName(), nav.getId()));

			}
			WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
			ServletContext servletContext = webApplicationContext.getServletContext();
			InitCacheData.getInstance().initNavTypeData(servletContext);
			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	/**
	 * @description 删除数据
	 * @author fengya
	 * @date 2016-3-15 下午03:56:08
	 * @param deleteIds
	 * @return
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds) throws Exception {
		User user = (User) session.getAttribute("loginUser");
		this.navManageService.removeData(user, deleteIds);
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext.getServletContext();
		InitCacheData.getInstance().initNavTypeData(servletContext);
		return Constant.DEL_SUCCESS;
	}

}
